package main

import (
	"encoding/json"
	"fmt"
)

type Persion struct {
	Name string `json:"-"`           // 要序列化，必须导出属性
	Age  int    `json:"a,omitempty"` // json标签，序列化时，json的key是age
	// `库要求的名称:"p1,p2"`
}

func main() {
	b, err := json.Marshal([]Persion{
		{"tom", 18},
		{"jerry", 20},
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("%T %[1]v, %s\n", b, string(b))
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~")
	// var data = `[{"n":"tom","a":18},{"n":"jerry","a":20}]`
	var data = string(b)
	var i []Persion
	err = json.Unmarshal([]byte(data), &i)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%T %+[1]v\n", i)
	fmt.Println(i[0].Name, i[0].Age) // tom 18
}
